package in.muton.edushare.schedules.widgets;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateHelper {
	public DateHelper(Date d) {
		date = d;
	}

	private void add(int param, int num) {
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		c.add(param, num);
		date = c.getTime();
	}

	private void set(int param, int num){
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		c.set(param, num);
		date = c.getTime();
	}
	
	private int get(int param){
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		return c.get(param);
	}
	
	public DateHelper addSecs(int seconds) {
		add(Calendar.SECOND, seconds);
		return this;
	}
	
	public DateHelper setSecs(int seconds) {
		set(Calendar.SECOND, seconds);
		return this;
	}
	
	public int getSecs(){
		return get(Calendar.SECOND);
	}

	public DateHelper addMins(int mins) {
		add(Calendar.MINUTE, mins);
		return this;
	}
	
	public DateHelper addWeeks(int weeks){
		add(Calendar.WEEK_OF_YEAR, weeks);
		return this;
	}
	
	public DateHelper setMins(int mins) {
		set(Calendar.MINUTE, mins);
		return this;
	}
	
	public int getMins(){
		return get(Calendar.MINUTE);
	}
	
	public DateHelper addHours(int hours) {
		add(Calendar.HOUR, hours);
		return this;
	}
	
	public DateHelper setHours(int hours) {
		set(Calendar.HOUR, hours);
		return this;
	}
	
	public int getHours(){
		return get(Calendar.HOUR);
	}

	public DateHelper addDays(int days) {
		add(Calendar.DAY_OF_MONTH, days);
		return this;
	}
	
	public DateHelper setDays(int days) {
		set(Calendar.DAY_OF_MONTH, days);
		return this;
	}
	
	public int getDays(){
		return get(Calendar.DAY_OF_MONTH);
	}

	public DateHelper addMonths(int months) {
		add(Calendar.MONTH, months);
		return this;
	}
	
	public DateHelper setMonths(int months) {
		set(Calendar.MONTH, months);
		return this;
	}
	
	public int getMonth(){
		return get(Calendar.MONTH);
	}

	public DateHelper addYears(int years) {
		add(Calendar.YEAR, years);
		return this;
	}
	
	public DateHelper setYears(int years) {
		set(Calendar.YEAR, years);
		return this;
	}
	
	public int getYear(){
		return get(Calendar.YEAR);
	}

	public Date getDate() {
		return date;
	}
	
	public static boolean timeAfter(Date date1, Date date2){
		return date1.getTime() > date2.getTime();
	}
	
	public static boolean timeBefore(Date date1, Date date2){
		return !timeAfter(date1, date2);
	}

	public static boolean after(Date date1, Date date2) {
		GregorianCalendar gc1 = new GregorianCalendar();
		gc1.setTime(date1);
		GregorianCalendar gc2 = new GregorianCalendar();
		gc2.setTime(date2);

		if (gc1.get(Calendar.YEAR) > gc2.get(Calendar.YEAR))
			return true;
		else if (gc1.get(Calendar.YEAR) == gc2.get(Calendar.YEAR)
				&& gc1.get(Calendar.MONTH) > gc2.get(Calendar.MONTH)) {
			return true;
		} else if (gc1.get(Calendar.YEAR) == gc2.get(Calendar.YEAR)
				&& gc1.get(Calendar.MONTH) == gc2.get(Calendar.MONTH)
				&& gc1.get(Calendar.DATE) > gc2.get(Calendar.DATE)) {
			return true;
		}
		return false;
	}

	public static boolean before(Date date1, Date date2) {
		GregorianCalendar gc1 = new GregorianCalendar();
		gc1.setTime(date1);
		GregorianCalendar gc2 = new GregorianCalendar();
		gc2.setTime(date2);

		if (gc1.get(Calendar.YEAR) < gc2.get(Calendar.YEAR))
			return true;
		else if (gc1.get(Calendar.YEAR) == gc2.get(Calendar.YEAR)
				&& gc1.get(Calendar.MONTH) < gc2.get(Calendar.MONTH)) {
			return true;
		} else if (gc1.get(Calendar.YEAR) == gc2.get(Calendar.YEAR)
				&& gc1.get(Calendar.MONTH) == gc2.get(Calendar.MONTH)
				&& gc1.get(Calendar.DATE) < gc2.get(Calendar.DATE)) {
			return true;
		}
		return false;
	}

	public static boolean sameDay(Date date1, Date date2) {
		GregorianCalendar gc1 = new GregorianCalendar();
		gc1.setTime(date1);
		GregorianCalendar gc2 = new GregorianCalendar();
		gc2.setTime(date2);

		if (gc1.get(Calendar.YEAR) == gc2.get(Calendar.YEAR)
				&& gc1.get(Calendar.MONTH) == gc2.get(Calendar.MONTH)
				&& gc1.get(Calendar.DATE) == gc2.get(Calendar.DATE)) {
			return true;
		}
		return false;
	}
	
	public int getDayOfWeek(){
		return getDayOfWeek(date);
	}
	
	public static int getDayOfWeek(Date date){
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		return gc.get(Calendar.DAY_OF_WEEK);
	}
	
	public static int getDayOfMonth(Date date){
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		return gc.get(Calendar.DAY_OF_MONTH);
	}

	private Date date = null;
}
